import java.util.LinkedList;
import java.util.Queue;

class MyStack {


    private Queue<Integer> qu1;
    private Queue<Integer> qu2;
    public MyStack() {
        qu1 = new LinkedList<>();
        qu2 = new LinkedList<>();
    }

    public void push(int x) {
        if(!qu1.isEmpty()) {
            qu1.offer(x);
        }else if(!qu2.isEmpty()) {
            qu2.offer(x);
        }else{
            //当两个队列都为空的时候，放到第一个里面
            qu1.offer(x);
        }
    }

    public int pop() {
        if(empty()) {
            return -1;//两个队列都为空的时候，返回-1
        }
        if(!qu1.isEmpty()) {
            int size = qu1.size();
            for(int i = 0;i <size-1;i++) {
                int x = qu1.poll();
                qu2.offer(x);
            }
            //返回剩下的元素
            return qu1.poll();
        }else {
            int size = qu2.size();
            for(int i = 0;i < size-1;i++) {
                int x = qu2.poll();
                qu1.offer(x);
            }
            //返回剩下的元素
            return qu2.poll();
        }
    }
    //获取栈顶元素 但是不删除  peek()
    public int top() {
        if(empty()) {
            return -1;//两个队列都为空的时候，返回-1
        }

        if(!qu1.isEmpty()) {
            int x = -1;
            int size = qu1.size();
            for(int i = 0;i < size;i++) {
                x = qu1.poll();
                qu2.offer(x);
            }
            //返回剩下的元素
            return x;
        }else {
            int x = -1;
            int size = qu2.size();
            for(int i = 0;i < size;i++) {
                x = qu2.poll();
                qu1.offer(x);
            }
            //返回剩下的元素
            return x;
        }

    }
    //两个队列都为空的时候，整个栈为空
    public boolean empty() {
        if(qu1.isEmpty() && qu2.isEmpty()) {
            return true;
        }
        return false;
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */